<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="8">
          <a-col :md="5" :sm="5">
            <a-form-item label="标题">
              <a-input placeholder="请输入标题" v-model="queryParam.title"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="$refs.table.refresh(true)" icon="search">查询</a-button>
              <a-button
                @click="() => {this.$objToReset(queryParam),$refs.table.refresh(true)}"
                icon="reload"
                style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
      <div class="table-operator">
        <a-button
          @click="add()"
          type="primary"
          icon="plus"
          v-wx-role="51501">添加
        </a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <a-menu slot="overlay">
            <a-menu-item>
              <a-popconfirm
                title="确定删除吗?"
                @confirm="() => handleDelete()"
                v-wx-role="51504">
                <a>
                  <a-icon type="delete"/>
                  删除
                </a>
              </a-popconfirm>
            </a-menu-item>
          </a-menu>
          <a-button style="margin-left: 8px">
            批量操作
            <a-icon type="down"/>
          </a-button>
        </a-dropdown>
      </div>
      <s-table
        ref="table"
        rowKey="id"
        size="middle"
        :columns="columns"
        :alert="options.alert"
        :row-selection="options.rowSelection"
        :data="loadData">
        <template slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)" v-wx-role="51501">
            <a-icon type="edit"/>
            编辑
          </a>
          <a-divider type="vertical"/>
          <a @click="handelView(record)">
            <a-icon type="eye"/>
            查看</a>
          <a-divider type="vertical"/>
          <a-popconfirm title="确定删除吗?" @confirm="() =>handleDelete(record.id)" v-wx-role="51504">
            <a>删除</a>
          </a-popconfirm>
        </template>
      </s-table>
    </div>
  </a-card>
</template>

<script>
import { STable, Ellipsis, TagSelect, StandardFormRow } from '@/components'
import { deleteAction, postAction } from '@/api/manage'
import { baseMixin } from '@/store/app-mixin'
import NoticeModalEdit from './NoticeModalEdit'
import NoticeView from './NoticeView'

export default {
  name: 'NoticeList',
  mixins: [baseMixin],
  components: { STable, Ellipsis, StandardFormRow, TagSelect, NoticeModalEdit, NoticeView },
  data () {
    return {
      visible: false,
      // 查询条件
      queryParam: {
        title: '',
        msgCateType: ''
      },
      selectedRowKeys: [],
      // 表头
      columns: [
        {
          title: '标题',
          dataIndex: 'title'
        },
        {
          title: '消息类型',
          dataIndex: 'msgCategory',
          customRender: (text) => this.$sysDictFilter(text, 'msgCategory')
        },
        {
          title: '发布人',
          dataIndex: 'createBy'
        },
        {
          title: '优先级',
          dataIndex: 'priority',
          customRender: (text) => this.$sysDictFilter(text, 'priority')
        },
        {
          title: '发布时间',
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        console.log('loadData.parameter', parameter)
        return postAction(this.url.list, Object.assign(parameter, this.queryParam))
          .then(res => {
            return res.result
          })
      },
      options: {
        alert: {
          clear: () => {
            this.selectedRowKeys = []
          }
        },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange,
          type: 'checkbox'
        }
      },
      url: {
        list: '/system/sysNotice/list',
        delete: '/system/sysNotice/delete'
      }
    }
  },
  mounted () {
  },
  methods: {
    add (record) {
      this.$router.push({ name: 'NoticeModalEdit' })
    },
    // 用户编辑
    handleEdit (record) {
      // this.$refs.modalEdit.edit(record);
      this.$router.push({ name: 'NoticeModalEdit', query: { id: record.id } })
    },
    searchQuery () {
      this.$refs.table.refresh(true)
    },
    // 保存刷新
    modalEditOk () {
      this.$refs.table.refresh()
    },
    // 查看
    handelView (record) {
      this.$router.push({ name: 'NoticeView', query: { id: record.id } })
    },
    // 删除
    handleDelete (record) {
      if (!record) {
        // 如果没有传入，那么批量删除
        record = this.selectedRowKeys.join(',')
      }
      deleteAction(this.url.delete, { ids: record }).then((resp) => {
        this.$message.success('删除成功!')
        this.$refs.table.refresh(true)
      })
    },
    onSelectChange (selectedRowKeys) {
      console.log('selectedRowKeys changed: ', selectedRowKeys)
      this.selectedRowKeys = selectedRowKeys
    }
  },
  watch: {}
}
</script>
<style scoped>
</style>
